var recommendservice = require("../../../apiservice/recommend/recommendservice");
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
const common = require("../modify/common");

// pages/promotion/list/list.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		starttime: '', //开始时间
		endtime: '', //结束时间
		stateOpt: 0, // 0 全部 1已过期 2三天内 3已达成
		recommendlist: [],
		alphabet: [],
		contactid: '',
		noshow: "系统没有找到您要的结果，请重新查询",
		height: 0,
		rows: 1
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {

		if (options.contactid) {
			this.setData({
				contactid: options.contactid,

			})
		}
		//debugger;
		var starttime = common.formatTime(new Date(), true);
		var endtime = common.formatTime(new Date(), false);
		let that = this;
		this.QueryRecommendListByConditions(starttime, endtime, this.data.contactid, 0, null)
			.then(function (recommendlist) {
				that.BindingData(recommendlist);
				console.log(recommendlist);
				that.setData({
					starttime: starttime, //开始时间
					endtime: endtime, //结束时间
				});


				var res = wx.getSystemInfoSync();
				that.setData({
					height: res.windowHeight-172 /// 4
				});

				console.log("height:" + that.data.height);
			}).catch(function (err) {
				console.log(err.data.message);
				var res = wx.getSystemInfoSync();
				that.setData({
					height: res.windowHeight-172 /// 4
				});
			})
	},
	// onSearch: async function(e) {
	// 	//console.log(e);
	// 	wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
	// 		title: '加载中',
	// 		icon: 'loading'
	// 	});
	// 	var recommendlist = {};
	// 	var ishospital = false;
	// 	if (this.data.stateOpt == 3) {
	// 		ishospital = true
	// 	}
	// 	if (this.data.stateOpt == 0) {
	// 		recommendlist = await this.QueryRecommendListByConditions(e.detail.starttime, e.detail.endtime, this.data.contactid,
	// 			this.data.stateOpt, null);
	// 	} else {
	// 		recommendlist = await this.QueryRecommendListByConditions(e.detail.starttime, e.detail.endtime, this.data.contactid,
	// 			this.data.stateOpt, ishospital);
	// 	}
	// 	this.BindingData(recommendlist);
	// 	this.setData({
	// 		starttime: e.detail.starttime, //开始时间
	// 		endtime: e.detail.endtime, //结束时间
	// 	});
	// 	wx.hideLoading();
	// },

	onSearch: function (e) {
		//console.log(e);
		wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
			title: '加载中',
			icon: 'loading'
		});
		//var recommendlist = {};
		var ishospital = false;
		if (this.data.stateOpt == 3) {
			ishospital = true
		}
		if (this.data.stateOpt == 0) {
			this.QueryRecommendListByConditions(e.detail.starttime, e.detail.endtime, this.data.contactid,
				this.data.stateOpt, null).then((recommendlist) => {
					this.BindingData(recommendlist);
					this.setData({
						starttime: e.detail.starttime, //开始时间
						endtime: e.detail.endtime, //结束时间
					});
					wx.hideLoading();
				}).catch(error => {
					this.BindingData([]);
					wx.hideLoading();
					
				});
		} else {
			this.QueryRecommendListByConditions(e.detail.starttime, e.detail.endtime, this.data.contactid,
				this.data.stateOpt, ishospital).then((recommendlist) => {
					this.BindingData(recommendlist);
					this.setData({
						starttime: e.detail.starttime, //开始时间
						endtime: e.detail.endtime, //结束时间
					});
					wx.hideLoading();
				}).catch(error => {
					this.BindingData([]);
					wx.hideLoading();
				});
		}

	},
	enterDetail: function (e) {
		//console.log(e)
		var id = e.currentTarget.id;
		//console.log(id)
		wx.navigateTo({
			url: '/pages/promotion/detail/detail?id=' + id
		})
	},
	onChangeState: function (e) {

		this.setData({
			stateOpt: e.detail.index
		});

		this.refreshLoadPage(this);

	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		//console.log(this.data.height);
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},
	addRecommond: function () {
		let that = this;
		wx.navigateTo({
			url: '/pages/promotion/add/add',
			events: {
				refreshPage: function () {
					//debugger;
					that.refreshLoadPage(that);
				}
			},
			success() {

			}
		})
	},

	refreshLoadPage: async function (that) {
		wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”
			title: '加载中',
			icon: 'loading'
		});
		let recommendlist = {};
		let ishospital = false;
		try {
			if (that.data.stateOpt == 3) //已完成
			{
				ishospital = true;
				recommendlist = await that.QueryRecommendListByConditions(that.data.starttime, that.data.endtime, that.data.contactid,
					that.data.stateOpt, ishospital);
			} else if (that.data.stateOpt == 0) //全部
			{
				recommendlist = await that.QueryRecommendListByConditions(that.data.starttime, that.data.endtime, that.data.contactid,
					that.data.stateOpt, null);
			} else if (that.data.stateOpt == 1) //过期
			{
				recommendlist = await that.QueryRecommendListByConditions(that.data.starttime, that.data.endtime, that.data.contactid,
					that.data.stateOpt, ishospital);
			} else //3天内
			{
				recommendlist = await that.QueryRecommendListByConditions(that.data.starttime, that.data.endtime, that.data.contactid,
					that.data.stateOpt, ishospital);
			}
			that.BindingData(recommendlist);
			wx.hideLoading();
		} catch (error) {
			that.BindingData([]);
			wx.hideLoading();
		}
		
	},

	

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},
	BindingData: function (recommendlist) {
		console.log(recommendlist);
		if(recommendlist.length>0)
		{
			for (let index in recommendlist) {
				this.setData({ ['alphabet[' + index + ']']: { initial: recommendlist[index].groupname, cells: recommendlist[index].grouplist } });
			}
		}else{
			this.setData({ alphabet: [] });
		}
	},
	// 获取推广记录
	QueryRecommendListByConditions: async function (starttime, endtime, contactid, statecode, ishospital) {

		var result = await recommendservice.QueryRecommendListByConditions(starttime, endtime, contactid, statecode,
			ishospital);
		this.setData({
			rows: result.data.length
		});

		return result.data;

	}
})
